package pos.data;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;



import pos.domain.Evento;


public class JDBCEventoDAO implements IEventoDAO{
	
	
	private Connection conne;

	IUsuarioDAO udao;
	

	public JDBCEventoDAO() {
		conne = ConnectionManager.getInstance().checkOut();
		udao = new JDBCUsuarioDAO();
		

	}

	protected void finalize() {
		ConnectionManager.getInstance().checkIn(conne);
	}
	
	
	public List selectAllEvento() {

		PreparedStatement stmt = null;
		List searchResults = new LinkedList();
		ResultSet result = null;

		try {

			String sql = "SELECT* FROM Evento";
			stmt = conne.prepareStatement(sql);
			result = stmt.executeQuery();

			while (result.next()) {

				Evento e = new Evento();
				Integer moid = result.getInt("eventoOID");

				e.setEventoUsuarios(udao.selectAllUsuariosEvento(conne, moid));

				e.setEventoFecha(result.getString("fecha"));
				e.setEventoNumAsistentes(result.getInt("numAsistentes"));
				searchResults.add(e);
			}

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
				this.finalize();
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}

		}
		return searchResults;
	}

	
	
	
	public List selectEventoBarrio(Connection conn, Integer barrioOID) {

		PreparedStatement stmt = null;
		List searchResults = new LinkedList();
		ResultSet result = null;

		try {

			String sql = "SELECT * FROM Evento WHERE barrioOID  LIKE '"
					+ barrioOID + "'";
			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();

			while (result.next()) {

				Evento e = new Evento();
				Integer moid = result.getInt("eventoOID");

			
				e.setEventoUsuarios(udao.selectAllUsuariosEvento(conn, moid));

				e.setEventoFecha(result.getString("fecha"));
				e.setEventoNumAsistentes(result.getInt("numAsistentes"));
				searchResults.add(e);
			}

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {

			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}

		}
		return searchResults;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
